package com.test.nnooolwdeaer.dialog_wooden;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;

import com.test.nnooolwdeaer.R;
import com.test.nnooolwdeaer.constant_wooden.Constant_wooden;
import com.test.nnooolwdeaer.util_wooden.http_wooden.HttpManager_wooden;

import java.util.Objects;

public class DropDialog_wooden extends Dialog {

    private final Thread countDownThread;
    private final Handler handler;
    
    private Button confirmBtn;
    
    public DropDialog_wooden(@NonNull Context context) {
        super(context);
        handler = new Handler(Looper.getMainLooper());
        countDownThread = new Thread(() -> {
            try {
                int count = 10;

                while (count-->0){
                    int finalCount = count;
                    if (isShowing()) handler.post(() -> confirmBtn.setText(context.getString(R.string.drop_confirm,finalCount)));
                    Thread.sleep(1000);
                }

                if (isShowing()){
                    handler.post(() -> {
                        confirmBtn.setAlpha(1f);
                        confirmBtn.setText(context.getString(R.string.confirm));
                        confirmBtn.setOnClickListener(v -> {
                            try {
                                HttpManager_wooden.getInstance_wooden().saveUserBuriedPoint("DIALOG_DELETE_DATA_DELETE_CLICK");
                                dismiss();
                                HttpManager_wooden.getInstance_wooden().dropUserData();
                            }catch (Exception e){
                                if (Constant_wooden.Debug) e.printStackTrace();
                            }
                        });
                    });
                }
            }catch (Exception e){
                if (Constant_wooden.Debug) e.printStackTrace();
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_drop_wooden);
        initView_wooden();
    }

    @SuppressLint("SetTextI18n")
    private void initView_wooden(){

        try {
            Objects.requireNonNull(getWindow()).setBackgroundDrawableResource(R.color.trans);
            getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            setCancelable(false);
        }catch (Exception e){
            if (Constant_wooden.Debug) e.printStackTrace();
        }

        Button returnBtn = findViewById(R.id.drop_btn_return);
        confirmBtn = findViewById(R.id.drop_btn_confirm);
        
        returnBtn.setOnClickListener(v -> {
            HttpManager_wooden.getInstance_wooden().saveUserBuriedPoint("DIALOG_DELETE_DATA_CANCEL_CLICK");
            dismiss();
        });

    }

    @Override
    public void show() {
        super.show();
        countDownThread.start();
    }
}
